home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / door / twview93.zip / GSDATA.INC < prev    next >
Text File  |  1992-05-02  |  9KB  |  318 lines

  1. {GSDATA.INC}
  2. const
  3.   SectorFieldMarker = 'Sector data starts here...';
  4.   
  5. procedure writeSectors( var g : text; var ss : SectorArray );
  6. var
  7.   s : sector;
  8.   i : 0..maxWarps;
  9. begin
  10.   writeln( g, SectorFieldMarker);
  11.   for s := 1 to maxSector do
  12.     with ss[s] do
  13.       if (number <> Unexplored) or (PortType <> NotAPort) or (etc <> Nothing) then
  14.       begin
  15.         write( g, s : 5, number:3 );
  16.         for i := 1 to number do
  17.           write( g, data[i] : 5);
  18.         write( g, portType : 3 );
  19.         write( g, etc : 6 );
  20.         writeln( g );
  21.       end; {for if}
  22. end; {writeSectors}
  23.  
  24. procedure WriteDock( var g : text; d : integer );
  25. begin
  26.   writeln( g, 'SpaceDock is ', d : 5 );
  27. end;
  28.  
  29. procedure WriteNotes( var g : text; var n : NoteList );
  30. var
  31.   i : 1..MaxNote;
  32. begin
  33.   writeln( g, n.top, '  <- number of notes' );
  34.   if n.top > 0 then
  35.     for i := 1 to n.top do
  36.       writeln( g, n.data[ i ].reference : 5, ' ', n.data[i].info );
  37. end; {WriteNotes}
  38.  
  39. procedure WritePorts( var g : text; var p : PortList );
  40. var
  41.   i : 1..MaxPorts;
  42. begin
  43.   writeln( g, p.top, '  <- number of Port Infos' );
  44.   if p.top > 0 then
  45.     for i := 1 to p.top do
  46.       with p.data[i] do
  47.         writeln( g, where : 5, amts[ Fuel ] : 8, amts[ Organics ] : 8, 
  48.                  amts[ Equipment ] : 8, usage[ Fuel ] : 4,
  49.                  usage[ Organics ] : 4, usage[ Equipment ] : 4, ' ',
  50.                  change[ Fuel ] : 5, ' ', change[ Organics ] : 5,
  51.                  ' ', change[ Equipment ] : 5 );
  52. end; {WritePorts}
  53.  
  54. const
  55.  DataFileIdentifier = '::Tradewars Data file::';
  56.  
  57. procedure SaveData( var g : text; var Space : TheVoid );
  58. begin
  59.   writeln(g, DataFileIdentifier );
  60.   writeDock( g, Space.dock );
  61.   writeNotes( g, Space.notes );
  62.   writePorts( g, space.ports );
  63.   writeSectors( g, space.sectors );
  64.   close( g );
  65. end; {SaveData}
  66.  
  67. procedure InitSectors( var s : SectorArray );
  68. var
  69.   r : sector;
  70. begin
  71.   for r := 1 to maxSector do
  72.     with s[r] do
  73.       begin
  74.         number  := UnExplored;
  75.         portType:= NotAPort;
  76.         etc     := Nothing;
  77.       end; {for with}
  78. end; {Init Sectors}
  79.  
  80.  
  81. function bval( line : string; var n : integer) : boolean;
  82. { convert  nonnegative numeric value at front of line into n; return whether
  83. conversion was successful. }
  84. var
  85.   i : integer;
  86.   error : boolean;
  87.  
  88. begin
  89.   i := 1;
  90.   n := 0;
  91.   Error := true;
  92.   while ( i <= length( line ) ) do
  93.     if line[i] = ' ' then
  94.       i := i + 1
  95.     else if line[ i ] in ['0'..'9'] then
  96.       begin
  97.         Error := false;
  98.         n := 10 * n + ord( line[ i ] ) - ord('0');
  99.         i := i + 1;
  100.       end
  101.     else
  102.       i := length( line ) + 1;
  103.   bval := error;
  104. end;
  105.  
  106. procedure InitPorts( var p : portList );
  107. const
  108.   empty  : goodsArray = ( 0, 0, 0 );
  109. var
  110.   i : portIndex;
  111. begin
  112.   p.top := 0;
  113.   for i := 1 to MaxPorts do
  114.     p.data[i].amts := empty;
  115. end;
  116.  
  117. procedure InitSpace( var s : TheVoid );
  118. begin
  119.   s.dock  := 0;
  120.   s.notes.top := 0;
  121.   InitPorts( s.ports );
  122.   InitSectors( s.sectors );
  123. end;
  124.  
  125. procedure ReadSectors( var h : text; var ss : SectorArray );
  126. var
  127.   r : sector;
  128.   i : 0..maxSector;
  129.   temp,
  130.   etccount,
  131.   portcount,
  132.   sectorcount :integer;
  133.   line : string;
  134. begin
  135.   portcount := 0; sectorcount := 0; etccount := 0;
  136.   readln( h, line);   { "Sector data starts here..." }
  137.   if line <> SectorFieldMarker then
  138.     writeln('Sector data missing?  Found ', line )
  139.   else
  140.     while not eof( h ) do
  141.       begin
  142.         read( h, r );
  143.         sectorcount := sectorcount + 1;
  144.         with ss[r] do
  145.           begin
  146.             read( h, number );
  147.             for i := 1 to number do
  148.               begin
  149.                 read( h, temp );
  150.                 if (temp >= 1) and (temp <= maxSector) then
  151.                   data[ i ] := temp
  152.                 else
  153.                   writeln('bad data for sector ', r);
  154.               end; {for}
  155.             read( h, temp );
  156.             if temp <> NotAPort then
  157.               PortType := temp;
  158.             if PortType <> NotAPort then
  159.               portcount := portcount + 1;
  160.             read( h, temp );
  161.             etc := etc or temp;
  162.             if etc <> Nothing then
  163.               etccount := etccount + 1;
  164.           end; {with}
  165.         readln( h );
  166.       end; {while}
  167.   writeln('Finished reading ', sectorcount, ' sectors, ',
  168.            portcount, ' ports, ', etccount, ' etc.');
  169. end; {ReadSectors}
  170.  
  171. procedure ReadDock( var f : text; var d : SectorIndex );
  172. begin
  173.   if d = 0 then
  174.     begin
  175.       skip( f, 12);    { "SpaceDock is" }
  176.       readln( f, d );
  177.       if d = 0 then
  178.         writeln('Space Dock location unknown')
  179.       else
  180.         writeln('Space Dock location ', d );
  181.     end
  182.   else  {already known, just skip line}
  183.     readln( f );
  184. end; {ReadDock}
  185.  
  186. procedure ReadNotes( var f : text; var sn : NoteList );
  187. var
  188.   NoteIndex,
  189.   i : 0..MaxNote;
  190.   more,
  191.   j : integer;
  192.   n : string;
  193. begin
  194.   readln( f, more );
  195.   if more > 0 then
  196.     for i := 1 to more do
  197.       begin
  198.         readln( f, n );
  199.         if not bval( copy( n, 1, 5 ), j ) then
  200.           begin
  201.             NoteIndex := NoteNumber( j );         { is this note new? }
  202.             if NoteIndex = 0 then                 { yes, so add to    }
  203.               if sn.top = MaxNote then
  204.                 writeln('Too many notes.  Can''t record information')
  205.               else
  206.                 begin                             { # notes, and work }
  207.                   sn.top := sn.top + 1;           { with that entry   }
  208.                   NoteIndex := sn.top;
  209.                 end; {if}
  210.             with sn.data[ NoteIndex ] do          { then store it     }
  211.               begin
  212.                 reference := j;
  213.                 info := copy( n, 7, NoteSize);
  214.               end {with}
  215.           end {if}
  216.         else
  217.           writeln('Error with note ', n );
  218.       end; {for}
  219.   writeln('Info for ', sn.top, ' notes ');
  220. end; {ReadNotes}
  221.  
  222. procedure ReadPorts( var f : text; var sp : PortList );
  223. const
  224.   debug = false;
  225. var
  226.   p, i : 0..MaxPorts;
  227.   location : sector;
  228.   more : integer;
  229. begin
  230.   readln( f, more );
  231.   if debug then write('Should get ', more, ' ports.');
  232.   if debug then writeln('Top currently ', sp.top );
  233.   if more > 0 then
  234.     for i := 1 to more do
  235.       begin
  236.         read( f, location );
  237.         p := portnumber( location );
  238.         if debug then write( location : 5, ' to ', p : 3 );
  239.         if p = 0 then
  240.           begin
  241.             sp.top := sp.top + 1;
  242.             p := sp.top;
  243.             if debug then write (' => ', p : 3, ' ');
  244.           end; {if}
  245.         sp.data[p].where := location;
  246.         with sp.data[ p ] do
  247.           begin
  248.             read( f, amts[ Fuel ], amts[ Organics ], amts[ Equipment ]);
  249.             if not eoln( f ) then
  250.                read( f, usage[Fuel], usage[Organics], usage[Equipment] )
  251.             else
  252.               begin
  253.                 usage[fuel] := 0;
  254.                 usage[organics] := 0;
  255.                 usage[equipment] := 0;
  256.               end;
  257.             if not eoln( f ) then
  258.               read( f, change[Fuel], change[Organics], change[Equipment] )
  259.             else
  260.               begin
  261.                 change[Fuel] := amts[Fuel];
  262.                 change[Organics] := amts[Organics];
  263.                 change[Equipment] := amts[Equipment];
  264.               end;
  265.             readln( f );
  266.           end; {with}
  267.       end; {if for}
  268.   writeln('Info for ', sp.top, ' ports');
  269. end; {ReadPorts}
  270.  
  271. procedure GetData( var space : TheVoid; var name : string );
  272. var
  273.   h : text;
  274.   line,
  275.   filename : string;
  276. begin
  277.   if name = '' then
  278.     begin
  279.       writeln('Hit carriage return if data base hasn''t been generated yet.');
  280.       write('Name of data file?  ');
  281.       readln( filename );
  282.     end {if}
  283.   else
  284.     filename := name;
  285.   if filename = '' then
  286.     writeln('Okay, initializing with empty space.')
  287.   else
  288.     begin
  289.       assign( h, filename );
  290. {$I-}
  291.       reset( h );
  292. {$I+}
  293.       if ioresult <> 0 then
  294.         begin
  295.           writeln('Sorry, there seems to be a problem finding the file ', filename );
  296.           writeln('Does it really exist?  Have you misspelled it?  Wrong default directory?');
  297.           writeln('Anyway, I''m not sure where to go from here, so I''m giving up.');
  298.           writeln('Sorry about that, but hey, if you can''t control your files...');
  299.           halt;
  300.         end; {if}
  301.       if pos( '.', filename) <> 0 then
  302.         name := copy( filename, 1, pos( '.', filename) - 1 )
  303.       else
  304.         name := '';
  305.       readln( h, line );
  306.       if line <> DataFileIdentifier then
  307.         begin
  308.           writeln('This is not a trade wars database file!');
  309.           halt;
  310.         end; {Error}
  311.       ReadDock( h, Space.dock );
  312.       ReadNotes( h, Space.notes );
  313.       ReadPorts( h, space.ports );
  314.       ReadSectors( h, space.sectors );
  315.       close( h );
  316.     end; {if filename <> '' }
  317. end; {GetData}
  318.